package com.swq.java.code.chapter13.code36;

import java.util.concurrent.*;

public class Machine implements Callable<Integer> {
    public Integer call() {
        int sum = 0;
        for (int a = 1; a <= 100; a++) { //计算从1加到100的和
            sum = sum + a;
            try {
                Thread.currentThread().sleep(20);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return sum;
    }

    public static void main(String args[]) throws Exception {
        FutureTask<Integer> task = new FutureTask<Integer>(new Machine());
        Thread threadMachine = new Thread(task);
        threadMachine.start();
        System.out.println("等待计算结果...");
        System.out.println("从1加到100的和：" + task.get());
        System.out.println("计算完毕");
    }
}